package co.edu.uniandes.mundo;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BoxLayout;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

import co.edu.uniandes.servicio.interfaces.IEventoDialogoConfirmar;

/**
 * 
 * @author Rafael Guillermo Blanco Banquez <rafaelg.blancob@gmail.com>
 *
 */
public class ConfirmarGUI extends JDialog{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Metodo Contructor
	 * @param padre
	 * @param modal
	 * @param mensaje
	 * @param botonCancelar
	 */
	public ConfirmarGUI(JFrame padre, boolean modal
			, String mensaje, String botonCancelar,
			String botonContinuar, final IEventoDialogoConfirmar iEventoDialogoConfirmar) {
		
		super(padre,modal);
		
		JButton okButton = new javax.swing.JButton(botonContinuar);
		okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				setVisible(false);
                dispose();
                if(iEventoDialogoConfirmar != null){
                	iEventoDialogoConfirmar.eventoContinuar();
                }
				
			}
		});
		
		JButton cancelButton = new javax.swing.JButton(botonCancelar);
		cancelButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				setVisible(false);
                dispose();
                if(iEventoDialogoConfirmar != null){
                	iEventoDialogoConfirmar.eventoCancelar();
                }
				
			}
		});
		// Cierra el Dialogo cuando preciosa escape
        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancelar");
        ActionMap actionMap = getRootPane().getActionMap();
        actionMap.put("cancelar", new AbstractAction() {

            public void actionPerformed(ActionEvent e) {            	
            	setVisible(false);
                dispose();
                if(iEventoDialogoConfirmar != null){
                	iEventoDialogoConfirmar.eventoCancelar();
                }
            }
        });
        
		JTextArea txtTexto = new JTextArea(3,35);
		JScrollPane scrollPane = new JScrollPane(txtTexto);
		txtTexto.setEditable(false);
		txtTexto.setText(mensaje);
		
		JPanel panelCompo = new JPanel();
		panelCompo.setLayout(new BoxLayout(panelCompo,BoxLayout.PAGE_AXIS));
		
		panelCompo.add(scrollPane);
		
		JPanel panelBotones = new JPanel(new FlowLayout());
		panelBotones.add(okButton);
		panelBotones.add(cancelButton);
		
		panelCompo.add(panelBotones);
		
		add(panelCompo);
		setSize(300, 300);
		setLocationRelativeTo(null);
		setVisible(true);
		repaint();
		pack();
		
	}
}
